part of '../repository.dart';

const _kOneTimePurchasedItemTransactionTypeValue = 'one_time_purchased_item';

class _OneTimePurchasedItemTransactionDto extends Equatable implements _TransactionDto {
  const _OneTimePurchasedItemTransactionDto._internal({
    required this.id,
    required this.timeDto,
    required this.appStoreUserId,
    required this.originalTransactionId,
    required this.purchaseItemSku,
    required this.offerTypeDto,
    required this.ownershipTypeDto,
    required this.test,
    required this.revocationInfoDto,
  });

  @override
  final String id;

  @override
  final _TimeDto timeDto;

  @override
  final String appStoreUserId;

  @override
  final String originalTransactionId;

  @override
  final String purchaseItemSku;

  @override
  final _TransactionOfferTypeDto offerTypeDto;

  @override
  final _TransactionOwnershipTypeDto ownershipTypeDto;

  @override
  final bool test;

  @override
  final _TransactionRevocationInfoDto? revocationInfoDto;

  @override
  String get type => _kOneTimePurchasedItemTransactionTypeValue;

  @override
  List<Object?> get props {
    return [
      id,
      timeDto,
      appStoreUserId,
      originalTransactionId,
      purchaseItemSku,
      offerTypeDto,
      ownershipTypeDto,
      test,
      revocationInfoDto,
    ];
  }

  factory _OneTimePurchasedItemTransactionDto.fromJson(Map<String, dynamic> json) {
    final timeDto = _TimeDto.fromString(json[_kTransactionTimeKey]);

    final offerTypeDto = _TransactionOfferTypeDto.fromString(json[_kTransactionOfferTypeKey]);

    final ownershipTypeDto =
        _TransactionOwnershipTypeDto.fromString(json[_kTransactionOwnershipTypeKey]);

    final revocationInfoJson = json[_kTransactionRevocationInfoKey] as Map<String, dynamic>?;
    _TransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _TransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    return _OneTimePurchasedItemTransactionDto._internal(
      id: json[_kTransactionIdKey],
      timeDto: timeDto,
      appStoreUserId: json[_kTransactionAppStoreUserIdKey],
      originalTransactionId: json[_kTransactionOriginalTransactionIdKey],
      purchaseItemSku: json[_kTransactionPurchaseItemSkuKey],
      offerTypeDto: offerTypeDto,
      ownershipTypeDto: ownershipTypeDto,
      revocationInfoDto: revocationInfoDto,
      test: json[_kTransactionTestKey],
    );
  }

  factory _OneTimePurchasedItemTransactionDto.fromEntity(
      AppStoreOneTimePurchasedItemTransaction transaction) {
    final timeDto = _TimeDto.fromEntity(transaction.time);
    final offerTypeDto = _TransactionOfferTypeDto.fromEntity(transaction.offerType);
    final ownershipTypeDto = _TransactionOwnershipTypeDto.fromEntity(transaction.ownershipType);

    _TransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto = _TransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    return _OneTimePurchasedItemTransactionDto._internal(
      id: transaction.id,
      timeDto: timeDto,
      appStoreUserId: transaction.appStoreUserId,
      originalTransactionId: transaction.originalTransactionId,
      purchaseItemSku: transaction.purchaseItemSku,
      offerTypeDto: offerTypeDto,
      ownershipTypeDto: ownershipTypeDto,
      revocationInfoDto: revocationInfoDto,
      test: transaction.test,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kTransactionTypeKey: type,
      _kTransactionIdKey: id,
      _kTransactionTimeKey: timeDto.toString(),
      _kTransactionAppStoreUserIdKey: appStoreUserId,
      _kTransactionOriginalTransactionIdKey: originalTransactionId,
      _kTransactionPurchaseItemSkuKey: purchaseItemSku,
      _kTransactionOfferTypeKey: offerTypeDto.toString(),
      _kTransactionOwnershipTypeKey: ownershipTypeDto.toString(),
      _kTransactionTestKey: test,
      _kTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
    };
  }

  @override
  AppStoreOneTimePurchasedItemTransaction toEntity() {
    return AppStoreOneTimePurchasedItemTransaction(
      id: id,
      time: timeDto.toEntity(),
      appStoreUserId: appStoreUserId,
      originalTransactionId: originalTransactionId,
      purchaseItemSku: purchaseItemSku,
      offerType: offerTypeDto.toEntity(),
      ownershipType: ownershipTypeDto.toEntity(),
      test: test,
      revocationInfo: revocationInfoDto?.toEntity(),
    );
  }
}
